﻿using UnityEngine;
using UnityEngine.UI;
using Jinndev.UI;

namespace Jinndev {

    public class TileHeightUI : BaseUIBehaviour {

        #region static
        public static readonly string PATH = "Prefab/UI/MapEditor/Tile/TileHeightUI";

        /// <summary>
        /// 编辑高度
        /// </summary>
        /// <param name="onConfirm">确定回调</param>
        public static TileHeightUI Create(float curHeight, System.Action<float> onConfirm) {
            var com = UIManager.Instance.Add<TileHeightUI>(PATH, UIParam.ModalPanel);
            com.Init(curHeight, onConfirm);
            return com;
        }
        #endregion


        public InputField inputHeight;
        public Text textError;

        private float curHeight;
        private System.Action<float> onConfirm;

        private void Awake() {
            textError.text = string.Empty;
        }

        private void Start() {
            inputHeight.Select();
        }

        private void Update() {
            if (Input.GetKeyDown(KeyCode.KeypadEnter)) {
                OnClickConfirm();
            }
        }

        private void Init(float curHeight, System.Action<float> onConfirm) {
            this.curHeight = curHeight;
            this.onConfirm = onConfirm;

            inputHeight.text = curHeight.ToString();
        }

        public void OnClickConfirm() {
            if (float.TryParse(inputHeight.text, out float height)) {
                height = Mathf.Round(height * 100) / 100;
                Destroy(gameObject);
                onConfirm?.Invoke(height);
            }
        }

    }

}